var express = require('express');
var router = express.Router();
const RestResult = require('../RestResult');
const log = require('../utils/logUtil').log();
const TagEntity = require("../models/Tag").TagEntity;

//添加
router.post('/addTag', function (req, res, next) {
    log.debug(req.body);
    let name = req.body.name;
    let tag1 = req.body.tag1;
    let tag2 = req.body.tag2;
    let tag3 = req.body.tag3;
    let tag4 = req.body.tag4;
    let resData = new RestResult();
    TagEntity.findOne({name: name}, function (err, doc) {
        let tagEntity = new TagEntity({
            name : name,
            tag1 : tag1,
            tag2 : tag2,
            tag3 : tag3,
            tag4 : tag4,
        });
        if (err) {
            resData.code = 1;
            resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
            res.send(resData);
        }
        else if (doc) {
            resData.code = 1;
            resData.errorReason = "歌名已存在";
            res.send(resData);
        }
        else {
            tagEntity.save(function (err, group) {
                if (err) {
                    resData.code = 1;
                    resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
                    res.send(resData);
                }
                else {
                    resData.code = 0;
                    resData.returnValue = group;
                    res.send(resData);
                }
            })
        }

    })
})
//删除
router.post('/deleteTag', function (req, res, next) {
    log.debug(req.body);
    let _id = req.body._id;
    let resData = new RestResult();
    TagEntity.remove({_id: _id}, function (err, doc) {
        if (err) {
            resData.code = 1;
            resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
            res.send(resData);
        }
        else {
            resData.code = 0;
            res.send(resData);
        }
    })
})
module.exports = router;